package com.breaking.second.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.breaking.second.R;
import com.breaking.second.activity.pager.adapter.ImageAdapter;
import com.breaking.second.databinding.ActivityGoodsDetailBinding;
import com.breaking.second.entity.BookEntity;
import com.breaking.second.entity.GoodsEntity;
import com.breaking.second.utils.compat.RecyclerCompat;
import com.breaking.second.utils.okhttp.OkhttpUtil;
import com.bumptech.glide.Glide;

import okhttp3.FormBody;

public class GoodsDetailActivity extends BaseActivity<ActivityGoodsDetailBinding> {
    private GoodsEntity goodsEntity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initIntent();
        initView();
    }

    private void initView() {
        binding.bug.setOnClickListener(this::trading);
    }

    private void trading(View view) {
        if (goodsEntity == null) return;
        FormBody.Builder form = new FormBody.Builder();
        form.add("buyerId", String.valueOf(pageableVo.getUserId()));
        form.add("goodsId", goodsEntity.getId());
        form.add("price", String.valueOf(goodsEntity.getPrice()));
        form.add("sellerId", goodsEntity.getTUserId());
        OkhttpUtil.Post("trading", form, new OkhttpUtil.CallbackListener(this::trading));
    }

    private void trading(BookEntity bookEntity) {
        Toast.makeText(this, bookEntity.getMsg(), Toast.LENGTH_SHORT).show();
        if (bookEntity.isCode()) {
            finish();
        }
    }

    private void initIntent() {
        Intent intent = getIntent();
        String id = intent.getStringExtra("id");
        if (id == null) finish();
        getGoodsData(id);
    }

    private void getGoodsData(String id) {
        FormBody.Builder form = new FormBody.Builder();
        form.add("goodsId", String.valueOf(id));
        OkhttpUtil.Get("goods/details", form, new OkhttpUtil.CallbackListener(this::getGoodsData));
    }

    private void getGoodsData(BookEntity entity) {
        if (entity.isCode()) {
            goodsEntity = entity.to(GoodsEntity.class);
            if (goodsEntity == null) return;
            initGoodsData(goodsEntity);
        }
    }

    private void initGoodsData(GoodsEntity goodsEntity) {
        Glide.with(this).load(goodsEntity.getAvatar()).placeholder(R.mipmap.ic_launcher).into(binding.avatar);

        binding.username.setText(goodsEntity.getUsername());
        binding.addr.setText(String.format("%s.%s", DateUtils.getRelativeTimeSpanString(Long.parseLong(goodsEntity.getCreateTime())).toString(), goodsEntity.getAddr()));
        binding.content.setText(goodsEntity.getContent());
        binding.price.setText(String.valueOf(goodsEntity.getPrice()));

        ImageAdapter imageAdapter = new ImageAdapter(this, goodsEntity.getImageUrlList());
        binding.images.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        binding.images.setAdapter(imageAdapter);
        RecyclerCompat.SpacesItemDecoration(binding.images);
    }
}
